﻿using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace CheckWebsite
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("=== 网站连接信息检查工具 ===");
            Console.WriteLine();

            while (true)
            {
                Console.Write("请输入要检查的网址 (输入 'quit' 退出): ");
                string? input = Console.ReadLine()?.Trim();

                if (string.IsNullOrEmpty(input) || input.ToLower() == "quit")
                {
                    break;
                }

                try
                {
                    // 确保URL格式正确
                    if (!input.StartsWith("http://") && !input.StartsWith("https://"))
                    {
                        input = "https://" + input;
                    }

                    Uri uri = new Uri(input);
                    string host = uri.Host;

                    Console.WriteLine($"\n正在检查: {host}");
                    Console.WriteLine(new string('=', 50));

                    // 创建日志文件名
                    string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
                    string logFileName = $"{timestamp}_{host.Replace(".", "_").Replace(":", "_")}.log";
                    string logFilePath = Path.Combine(Directory.GetCurrentDirectory(), logFileName);

                    var checker = new WebsiteChecker(logFilePath);
                    await checker.CheckWebsite(host);

                    Console.WriteLine($"\n检查结果已保存到: {logFileName}");
                    Console.WriteLine();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"错误: {ex.Message}");
                    Console.WriteLine();
                }
            }

            Console.WriteLine("感谢使用！");
        }
    }
}
